package com.google.code.hibernate.rest.method;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.mortbay.jetty.Connector;
import org.mortbay.jetty.Handler;
import org.mortbay.jetty.Server;
import org.mortbay.jetty.nio.SelectChannelConnector;
import org.mortbay.jetty.webapp.WebAppContext;

import com.google.code.hibernate.rest.ExampleRestInterface;

/**
 * 
 * @author wangzijian
 * 
 */
public abstract class MethodTestSupport {

	private static Server server;

	@BeforeClass
	public static void startServer() throws Exception {
		server = buildServerWith(defaultWebAppContext());
		server.start();
	}

	@AfterClass
	public static void stopServer() throws Exception {
		server.stop();
	}

	private static Handler defaultWebAppContext() {
		WebAppContext context = new WebAppContext(".", "/test");
		context.addServlet(ExampleRestInterface.class, "/Entity/*");
		return context;
	}

	public static Server buildServerWith(Handler handler) {
		Server server = new Server();
		Connector connector = new SelectChannelConnector();
		connector.setPort(80);
		server.addConnector(connector);
		server.setHandler(handler);
		return server;
	}

}
